package com.zzf.juc练习;

public class 死锁 {
    static Object lock1 = new Object(),lock2 = new Object();

    static void f1(){
        System.out.println(Thread.currentThread()+"准备获得锁1");
        synchronized (lock1){
            System.out.println(Thread.currentThread()+"获得锁1，等待获得锁2");
            synchronized (lock2){
                System.out.println(Thread.currentThread()+"获得锁2");
            }
            System.out.println(Thread.currentThread()+"释放锁2");
        }
        System.out.println(Thread.currentThread()+"释放锁1");
    }

    static void f2(){
        System.out.println(Thread.currentThread()+"准备获得锁2");
        synchronized (lock2){
            System.out.println(Thread.currentThread()+"获得锁2，等待获得锁1");
            synchronized (lock1){
                System.out.println(Thread.currentThread()+"获得锁1");
            }
            System.out.println(Thread.currentThread()+"释放锁1");
        }
        System.out.println(Thread.currentThread()+"释放锁2");
    }

    public static void main(String[] args) {
        new Thread(()->{
            f1();
        }).start();

        new Thread(()->{
            f2();
        }).start();
    }

}
